package org.makcro.sms.services.chat.base.service;

import java.text.MessageFormat;

import org.apache.log4j.Logger;
import org.makcro.sms.services.chat.model.Question;

import com.klikmee.sms.core.commons.filters.ProcessItemFilter;
import com.klikmee.sms.core.commons.queues.QueueItem;
import com.klikmee.sms.core.commons.queues.QueueItemProcessor;

public class RequestMessageItemProcessor implements QueueItemProcessor<Question> {

	// logger instance
	private Logger							logger	= Logger.getLogger( RequestMessageItemProcessor.class );

	/**
	 * Filter chain
	 */
	private ProcessItemFilter<Question>[]	filters;

	@Override
	public boolean process(QueueItem<Question> item) {
		// extract question from item and feed it to filter chain
		Question question = item.getPayload();
		boolean itemProcessed = false;

		for (ProcessItemFilter<Question> messageFilter : filters) {
			itemProcessed = messageFilter.process( question );

			if (itemProcessed) {
				logger.info( MessageFormat.format( "Successfull filter activation on [{0}]", messageFilter ) );
				break;
			}
		}
		return itemProcessed;
	}

	public void setFilters(ProcessItemFilter<Question>[] filters) {
		this.filters = filters;
	}

	public ProcessItemFilter<Question>[] getFilters() {
		return filters;
	}

}
